<template>
  <div class="con">
    <div class="user-profile">
      <div class="info">
        <van-image
          round
          :src="photo"
        />
        <h3 class="name">
          {{userInfo.name}}
          <br />
          <van-tag size="mini">申请认证</van-tag>
        </h3>
      </div>
      <van-row>
        <van-col span="8">
          <p>{{userInfo.art_count}}</p>
          <p>动态</p>
        </van-col>
        <van-col span="8">
          <p>{{userInfo.follow_count}}</p>
          <p>关注</p>
        </van-col>
        <van-col span="8">
          <p>{{userInfo.fans_count}}</p>
          <p>粉丝</p>
        </van-col>
      </van-row>
    </div>
    <van-row class="user-links">
      <van-col span="8">
        <van-icon
          name="newspaper-o"
          color="#7af"
        />我的作品
      </van-col>
      <van-col span="8">
        <van-icon
          name="star-o"
          color="#f00"
        />我的收藏
      </van-col>
      <van-col span="8">
        <van-icon
          name="tosend"
          color="#fa0"
        />阅读历史
      </van-col>
    </van-row>

    <van-cell-group class="user-group">
      <van-cell
        icon="edit"
        title="编辑资料"
        to="/user/profile"
        is-link
      />
      <van-cell
        icon="chat-o"
        title="小智同学"
        to="/user/chat"
        is-link
      />
      <van-cell
        icon="setting-o"
        title="系统设置"
        is-link
      />
      <van-cell
        @click="logout"
        icon="warning-o"
        title="退出登录"
        is-link
      />
    </van-cell-group>
  </div>
</template>

<script>
// 导入获取用户信息接口
import { getUserInfo } from '@/api/user'
import { mapMutations, mapGetters } from 'vuex'
export default {
  name: 'user-index',
  data () {
    return {
      // 登录人信息
      userInfo: {}
    }
  },
  computed: {
    ...mapGetters(['photo'])
  },
  activated () {
    console.log('我的组件缓存了')
  },
  created () {
    console.log('profile', this.$store)
    this.getUser()
  },
  methods: {
    ...mapMutations(['delUser', 'setPhoto']),
    // 退出登录
    logout () {
      /**
       * 1. 提示确认用户是否要退出登录=>避免误操作
       * 2. 调用接口服务端退出=》前端退出
       */
      this.$dialog.confirm({
        title: '提示',
        message: '确认退出黑马头条吗？'
      })
        .then(() => {
          // on confirm
          console.log('点了确定')
          // 前端退出=》删除存储的token
          this.delUser()
          this.$toast.success('退出成功')
          this.$router.replace('/login')
        })
        .catch(() => {
          // on cancel
          console.log('点了取消')
        })
    },
    // 获取登录人信息
    async getUser () {
      const { data } = await getUserInfo()
      // console.log(data)
      this.userInfo = data
      // 存储登录人头像
      this.setPhoto(data.photo)
    }
  }
}
</script>

<style scoped lang='less'>
.user {
  &-profile {
    width: 100%;
    height: 150px;
    display: block;
    background: #3296fa;
    color: #fff;
    .info {
      display: flex;
      padding: 20px;
      align-items: center;
      .van-image {
        width: 64px;
        height: 64px;
      }
      .name {
        font-size: 16px;
        font-weight: normal;
        margin-left: 10px;
      }
      .van-tag {
        background: #fff;
        color: #3296fa;
      }
    }
    p {
      margin: 0;
      text-align: center;
    }
  }
  &-group {
    margin-bottom: 15px;
    text-align: left;
  }
  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
}
</style>
